'''
类定义（Customer, Route, Chromosome）
全局变量定义
'''


class Customer:
    def __init__(self, id=0, x=0.0, y=0.0, delivery_demand=0.0, pick_up_demand=0.0,
                 begin_time=0.0, end_time=0.0, server_time=0.0):
        self.id = id  # 客户id
        self.x = x  # 客户x坐标
        self.y = y  # 客户y坐标
        self.delivery_demand = delivery_demand  # 客户送货量
        self.pick_up_demand = pick_up_demand  # 客户取货量
        self.begin_time = begin_time  # 开始服务时间
        self.end_time = end_time  # 结束服务时间
        self.server_time = server_time  # 服务时间


class Route:
    def __init__(self):
        self.customers = []  # 客户列表
        self.travel_dist = 0.0  # 行驶距离
        self.travel_time = 0.0  # 行驶时间
        self.wait_time = 0.0  # 等待时间
        self.delay_time = 0.0  # 延误时间
        self.delivery_capacity = 0.0  # 送货容量
        self.pick_up_capacity = 0.0  # 取货容量


class Chromosome:
    def __init__(self):
        self.routes = []  # 路线列表，存储Route类的实例
        self.f = [0.0] * 5  # 目标函数值数组
        self.box_f = [0] * 5  # 目标函数的盒子编号
        self.similarity = 0.0  # 相似度
        self.crowding = 0.0  # 拥挤度

    def __lt__(self, other):
        return ((self.f[0] < other.f[0]) or
                (self.f[0] == other.f[0] and self.f[1] < other.f[1]) or
                (self.f[0] == other.f[0] and self.f[1] == other.f[1] and self.f[2] < other.f[2]) or
                (self.f[0] == other.f[0] and self.f[1] == other.f[1] and self.f[2] == other.f[2] and
                 self.f[3] < other.f[3]))

    def __eq__(self, other):
        used = [[False for _ in range(customer_num + 1)] for _ in range(customer_num + 1)]
        for route in self.routes:
            for i in range(len(route.customers) - 1):
                used[route.customers[i]][route.customers[i + 1]] = not used[route.customers[i]][route.customers[i + 1]]

        for route in other.routes:
            for i in range(len(route.customers) - 1):
                used[route.customers[i]][route.customers[i + 1]] = not used[route.customers[i]][route.customers[i + 1]]

        for i in range(customer_num + 1):
            for j in range(customer_num + 1):
                if used[i][j]:
                    return False
        return True


# 全局变量
MAX = 300
FUNC_NUM = 5
INF = 9999999

MOSL_EP=[]
MOMA_EP=[]
final_EP=[]

max_capacity = 0  # 货车最大送货量
max_delay_time = []  # 最大延误时间
peer_time = []  # 客户之间的时间
peer_distance = []  # 客户之间的距离
customer = [Customer() for _ in range(MAX)]  # 客户信息
customer_num = 0  # 客户个数
vehicle_num = 0  # 车辆数量

b_distance = [0 for _ in range(MAX)]  # 累计行驶距离（包括当前客户）
a_distance = [0 for _ in range(MAX)]  # 之后的累计行驶距离（包括当前客户）

total_wait = [0 for _ in range(MAX)]  # 一条路线上的累计等待时间，等于前一个客户的累计等待加上当前客户的等待时间
total_delay = [0 for _ in range(MAX)]  # 一条路线上的累计延误时间，等于前一个客户的累计延误加上当前客户的延误时间
a_time = [0 for _ in range(MAX)]
l_time = [0 for _ in range(MAX)]
w_time = [0 for _ in range(MAX)]  # 每个客户的等待时间
max_wait = [0 for _ in range(MAX)]  # 用于存储每个客户点可以容忍的最大等待时间

# 定义取送货的相关数组
pi_plus = [[0 for _ in range(MAX)] for _ in range(2)]
pi_minus = [[0 for _ in range(MAX)] for _ in range(2)]
delta_plus = [[0 for _ in range(MAX)] for _ in range(2)]
delta_minus = [[0 for _ in range(MAX)] for _ in range(2)]
M_plus = [[0 for _ in range(MAX)] for _ in range(2)]
M_minus = [[0 for _ in range(MAX)] for _ in range(2)]

# ε值用于控制非支配解的数量和档案更新
epsilon = 0.05
# 存档大小，用于限制EP的大小
archive_size = 500
# 优化目标数量
N = 5

# 存储最佳解的列表
total_best = []

# 极端解数组，每个目标函数一个极端解
extreme = [Chromosome() for _ in range(FUNC_NUM)]

# 染色体集合，存储当前种群的所有染色体
chromosome = []

# 有效解集（EP），存储非支配解
EP = []

# 父代和子代的集合
parent = []
children = []

# 前沿集（F）和支配集（S）
F = []
S = []

# 迭代次数，控制算法的终止条件
ITER = 3000

# EP更新标志，用于标记EP是否在迭代过程中被更新
EP_flag = False
